package com.daotonginfo.generator.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.daotonginfo.generator.model.Column;
import com.daotonginfo.generator.model.TableInfo;

@Repository
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "generator")
@Data
public class SchemaDao {

    private Map<String, String> type;

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询表的信息
     *
     * @param dbName
     * @param tableName
     * @return
     */
    public TableInfo getTable(String dbName, String tableName, String prefix) {
        final String sql = "select * from information_schema.TABLES where table_name=? and table_schema=? ";
        return jdbcTemplate.queryForObject(sql, new Object[] { tableName, dbName }, (rs, i) -> {
            TableInfo table = new TableInfo();
            table.setComment(rs.getString("TABLE_COMMENT"));
            table.setTableName(tableName);
            table.setPrefix(prefix);
            table.setColumnList(getColumns(dbName, tableName));
            return table;
        });
    }

    /**
     * 获取表的各列
     *
     * @param dbName    数据库名
     * @param tableName 数据表名
     * @return
     * @throws SQLException
     */
    public List<Column> getColumns(String dbName, String tableName) throws SQLException {
        final String sql = "select * from information_schema.columns where table_name=? and table_schema=? ";
        return jdbcTemplate.query(sql, new Object[] { tableName, dbName }, (rs, i) -> {
            Column column = new Column();
            String comment = rs.getString("COLUMN_COMMENT").trim();
            // 如果备注为空，则用列名代替
            if (comment == null || comment.equals(""))
                comment = rs.getString("COLUMN_NAME").trim();
            column.setComment(comment);
            column.setColumnName(rs.getString("COLUMN_NAME").trim());
            column.setLength(rs.getInt("CHARACTER_MAXIMUM_LENGTH"));
            column.setType(getJavaType(rs.getString("DATA_TYPE")).trim());
            column.setIsnull(rs.getString("IS_NULLABLE"));
            column.setColumnType(rs.getString("COLUMN_TYPE"));
            column.setColumnKey(rs.getString("COLUMN_KEY"));
            column.setExtra(rs.getString("EXTRA"));
            return column;
        });

    }

    /**
     * 进行数据库与java类型之间的映射
     */
    public String getJavaType(String mysqlType) {
        String javaType = "String";
        if (type != null) {
            return type.get(mysqlType);
        }
        switch (mysqlType.toLowerCase()) {
        case "bigint":
            javaType = "Long";
            break;
        case "varchar":
            javaType = "String";
            break;
        case "date":
            javaType = "LocalDate";
            break;
        case "datetime":
            javaType = "LocalDateTime";
            break;
        case "time":
            javaType = "LocalTime";
            break;
        case "int":
            javaType = "Integer";
            break;
        case "timestamp":
            javaType = "Date";
            break;
        case "tinyint":
            javaType = "Integer";
            break;
        case "smallint":
            javaType = "Integer";
            break;
        case "mediumint":
            javaType = "Integer";
            break;
        case "bit":
            javaType = "Boolean";
            break;
        case "float":
            javaType = "Float";
            break;
        case "double":
            javaType = "Double";
            break;
        case "decimal":
            javaType = "BigDecimal";
            break;
        }
        return javaType;
    }

}
